<?php 

namespace app\admin\controller;
use app\common\model\ShopApply as ShopApplyModel;
use app\admin\controller\Base;
use app\admin\validate\ShopApplyValidate;
use app\admin\event\ShopApplyEvent;
use app\common\utils\JsonService;
use think\facade\View;

class ShopApply extends Base{


    /**
    * 编辑店铺申请
    * @return mixed
    */
    public function update(){
        if($this->request->isAjax()){
            $post = $this->request->post();
            /*
            $validate = new ShopApplyValidate;
            if(!$validate->scene('edit')->check($post)) {
                return JsonService::error($validate->getError());
            }
            */
            return ShopApplyEvent::updateShopApply($post);
        }

        $apply_id = $this->request->get('apply_id');

        $model = $this->findModel($apply_id);
        $province = getProvinceCity();
        $city = getProvinceCity($model['province_id']);
        $district = getProvinceCity($model['city_id']);
        return View::fetch('update',[
            'model'=>$model,
            'province'=>$province,
            'city'=>$city,
            'district'=>$district,
        ]);
    }



    /*
     * 店铺注册信息
     * */
    public function shop_register_info(){
        if($this->request->isAjax()){
            $post = $this->request->post();
            /*
            $validate = new ShopApplyValidate;
            if(!$validate->scene('edit')->check($post)) {
                return JsonService::error($validate->getError());
            }
            */
            return ShopApplyEvent::updateShopApply($post);
        }

        $shop_id = $this->request->get('shop_id');

        $model = $this->findModel(-1,['shop_id'=>$shop_id]);
        $province = getProvinceCity();
        $city = getProvinceCity($model['province_id']);
        $district = getProvinceCity($model['city_id']);
        return View::fetch('update',[
            'model'=>$model,
            'province'=>$province,
            'city'=>$city,
            'district'=>$district,
        ]);
    }


    /**
    * 店铺申请列表

    * @return mixed
    */
    public function index(){
        $dataProvider = ShopApplyEvent::shopApplyList();
        return View::fetch('index',['dataProvider'=>$dataProvider]);
    }

    /**
    * 查看店铺申请
    * @return mixed
    */
    public function view(){
        $apply_id = $this->request->get('apply_id');
        $model = $this->findModel($apply_id);
        return View::fetch('view',[
            'model'=>$model,
        ]);
    }


    /**
    * 得到模型
    *
    * @param int $id
    * @param array $where
    *
    * @return array|false|\PDOStatement|string|\think\Model
    */
    public function findModel($id,$where = [])
    {
        if (empty($id) && empty($where)) {
            throw new \think\exception\RouteNotFoundException();
        }
        return ShopApplyModel::findModel($id, $where);
    }

}
